import 'dart:io';

import 'package:app_tracking_transparency/app_tracking_transparency.dart';
import 'package:flutter/widgets.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:device_info_plus/device_info_plus.dart';

class JsApp {
  static const jsPP = "https://h5.cosmofusionly.xyz/privacyPolicy.html"; // 隐私协议
  static const jsTOS = "https://h5.cosmofusionly.xyz/termConditions.html"; // 用户协议
  static const jsGpTranslate = "https://translation.googleapis.com/language/translate/v2"; // GP 翻译
  
  static const jsIsDebugScheme = true; // 是否为测试域名 true：使用 http   false：使用 https
  static const jsHttpScheme = "test-app.bigegg.work"; // 请求域名
  static const jsHttpEventScheme = "log.bigegg.work"; // 埋点域名

  static const jsCodeSuccess = "0"; // 请求成功
  static const jsCodeFailed = "-200"; // 请求失败
  static const jsCodeInvalidToken = "100103"; // 无效的 token
  static const jsCodeNoToken = "10010301"; // 末提供token
  static const jsCodeTokenError = "10010302"; // token 格式错误
  static const jsCodeTokenHasExpired = "10010303"; // token 已过期
  static const jsCodeLoginOnOtherDevices = "10010304"; // 其他设备登录
  static const jsCodeCoinIsInsufficient = "20010009"; // 余额不足

  static const jsHttpEvent = "log/live-chat"; // 日志上报
  static const jsHttpAppConfig = "config/getAppConfig"; // Config 接口
  static const jsHttpLogin = "security/oauth"; // 登录接口
  static const jsHttpIsValidToken = "security/isValidToken"; // Token 校验接口
  static const jsHttpLogout = "security/logout"; // 退出登录接口
  static const jsHttpDeleteAccount = "user/deleteAccount"; // 删除账号接口
  static const jsHttpMember = "user/getUserInfo"; // 用户信息接口
  static const jsHttpSaveMember = "user/saveUserInfo"; // 用户信息保存接口
  static const jsHttpOSS = "user/oss/policy"; // 获取 Oss 上传权限
  static const jsHttpUpdateAvatar = "user/updateAvatar"; // 更新头像接口
  static const jsHttpCoinList = "coin/goods/search"; // 充值商品列表
  static const jsHttpRecharge = "coin/recharge/create"; // 创建充值订单接口
  static const jsHttpCheckRecharge = "coin/recharge/payment/ipa"; // 验单接口
  static const jsHttpReviewModeConsume = "coin/reviewModeConsume"; // 金币消耗接口

  static const fileToTempName = "applicationCacheDirectory";

  static String appName = "JumpScape";
  static String deviceModel = "";
  static String packageName = "";
  static String versionName = "";
  static String buildNumber = "";

  static String userId = "";

  static final reportSeason = ["Pornographic", "False gender", "Fraud", "Political sensitive", "Other"];

  static void initATT() async {
    try {
      await Future.delayed(const Duration(seconds: 2));
      final TrackingStatus status = await AppTrackingTransparency.trackingAuthorizationStatus;
      if (status == TrackingStatus.notDetermined) {
        await Future.delayed(const Duration(seconds: 2));
        await AppTrackingTransparency.requestTrackingAuthorization();
      }
    } catch (e) {
      debugPrint("[ATT] e=$e");
    }
  }

  // 版本
  static Future<String> getBuildNumber() async {
    if (buildNumber.isEmpty) {
      PackageInfo packageInfo = await PackageInfo.fromPlatform();
      buildNumber = packageInfo.buildNumber;
    }
    return buildNumber;
  }

  // 版本 例如1.0.0，读取实际配置的版本号
  static Future<String> getVersionName() async {
    if (versionName.isEmpty) {
      PackageInfo packageInfo = await PackageInfo.fromPlatform();
      versionName = packageInfo.version;
    }
    return versionName;
  }

  // 包名
  static Future<String> getPackageName() async {
    if (packageName.isEmpty) {
      PackageInfo packageInfo = await PackageInfo.fromPlatform();
      packageName = packageInfo.packageName;
    }
    return packageName;
  }

  // 设备型号
  static Future<String> getDeviceModel() async {
    if (deviceModel.isEmpty) {
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      if (Platform.isAndroid) {
        AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
        deviceModel = androidInfo.hardware;
      } else if (Platform.isIOS) {
        IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
        deviceModel = iosInfo.model;
      } else if (Platform.isMacOS) {
        MacOsDeviceInfo macOsInfo = await deviceInfo.macOsInfo;
        deviceModel = macOsInfo.model;
      } else {
        deviceModel = "other";
      }
    }
    return deviceModel;
  }

  static String getPlatform() {
    if (Platform.isAndroid) {
      return 'Android';
    } else if (Platform.isIOS) {
      return "iOS";
    } else if (Platform.isLinux) {
      return "Linux";
    } else if (Platform.isMacOS) {
      return "MacOS";
    } else if (Platform.isWindows) {
      return "Windows";
    } else {
      return "Other";
    }
  }

  static Future<String> getPlatformVer() async {
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await DeviceInfoPlugin().androidInfo;
      return androidInfo.version.release;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await DeviceInfoPlugin().iosInfo;
      return iosInfo.systemVersion;
    } else {
      return "Unknown";
    }
  }

  static final countrys = [
    "England",
    "America",
    "Canada",
    "Russia",
    "Egypt",
    "Greece",
    "France",
    "Germany",
    "Japan",
    "Australia",
    "Brazil",
    "Argentina",
    "Columbia",
    "Iraq",
    "Denmark",
    "Philippines",
    "Norway",
    "Switzerland",
    "Cambodia",
    "Burma",
    "Portugal",
    "Pakistan",
    "Bangladesh",
    "romania",
    "Malaysia",
    "Maldives",
    "Afghanistan",
    "Iceland",
    "Ireland",
  ];
}
